package com.mb.app.controller;

import com.mb.app.domain.TbSpecsCfg;
import com.mb.app.service.ITbSpecsCfgService;
import com.mb.common.controller.BaseController;
import com.mb.common.domain.ZtreeNodeDo;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 规格属性配置  前端控制器
 * </p>
 *
 * @author liliping
 * @since 2019-04-01
 */
@Controller
@RequestMapping("/app/specsCfg")
@Slf4j
public class TbSpecsCfgController extends BaseController {
    
    @Autowired
    private ITbSpecsCfgService specsCfgService;

    @GetMapping("/index")
    String index(){
        return "/app/specsCfg/specsCfg_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbSpecsCfg> list = specsCfgService.queryList(query);
        int total = specsCfgService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add(){
        return "/app/specsCfg/specsCfg_add";
    }


    @ResponseBody
    @PostMapping("/save")
    public R save(TbSpecsCfg TbSpecsCfg, HttpServletRequest request){
        TbSpecsCfg.setAddUserId(getUserId());
        int result = specsCfgService.save(TbSpecsCfg);
        return R.ok();
    }


    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("specsId") Long specsId, HttpServletRequest request){
        Long delUserId = getUserId();
        int result = specsCfgService.remove(specsId,delUserId);
        return R.ok();
    }

    @GetMapping("/dtl/{specsCfgId}")
    String dtl(@PathVariable("specsCfgId") Long specsCfg, Model model){
        model.addAttribute("specsCfgInfo",specsCfgService.getDtl(specsCfg));
        return "/app/specsCfg/specsCfg_info";
    }


    @GetMapping("/edit/{specsCfgId}")
    String edit(@PathVariable("specsCfgId") Long specsCfgId, Model model){
        model.addAttribute("specsCfgInfo",specsCfgService.getDtl(specsCfgId));
        return "/app/specsCfg/specsCfg_edit";
    }


    @ResponseBody
    @PostMapping("/update")
    public R update(TbSpecsCfg TbSpecsCfg, HttpServletRequest request){
        TbSpecsCfg.setUpdateUserId(getUserId());
        int result = specsCfgService.update(TbSpecsCfg);
        return R.ok();
    }

    /**
     * 产品分类是否存在
     */
    @PostMapping("/specsCfgNameExit")
    @ResponseBody
    boolean specsCfgsNameExit(@RequestParam Map<String, Object> params) {
        // 存在，不通过，false
        return !specsCfgService.specsCfgsNameExit(params);
    }

    @GetMapping("/specsTree/{pid}")
    public String specsTree(@PathVariable("pid") Long pid,Model model){
        Map<String,Object> map = new HashMap<String, Object>();
        model.addAttribute("pId",pid);
        return "/app/specsCfg/specsTree";
    }


    @ResponseBody
    @PostMapping("/getspecsTreeList")
    public List<ZtreeNodeDo> getspecsTreeList(@RequestParam("pId") Long pId){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("pId",pId);
        List<TbSpecsCfg> specsCfgs = specsCfgService.getSpecsList(map);
        List<ZtreeNodeDo> ztreeNodeDos = new ArrayList<ZtreeNodeDo>();
        TbSpecsCfg tbSpecsCfg = specsCfgService.getDtl(pId);
        ZtreeNodeDo ztreeNode = this.getZtreeNodeDo(tbSpecsCfg);
        ztreeNode.setOpen(true);
        ztreeNodeDos.add(ztreeNode);
        if (specsCfgs.size()> 0) {
            for (TbSpecsCfg specsCfg : specsCfgs) {
                ZtreeNodeDo ztreeNodeDo =this.getZtreeNodeDo(specsCfg);
                ztreeNodeDo.setOpen(true);
                ztreeNodeDos.add(ztreeNodeDo);
                map.put("pId",specsCfg.getSpecsId());
                List<TbSpecsCfg> specsList = specsCfgService.getSpecsList(map);
                if (specsList.size()> 0) {
                    for (TbSpecsCfg specsCfg1 : specsList) {
                        ZtreeNodeDo ztreeNodeDo1 =this.getZtreeNodeDo(specsCfg1);
                        ztreeNodeDos.add(ztreeNodeDo1);
                    }
                }
            }
        }
        return ztreeNodeDos;
    }

    public ZtreeNodeDo getZtreeNodeDo(TbSpecsCfg specsCfg) {
        ZtreeNodeDo ztreeNodeDo = new ZtreeNodeDo();
        ztreeNodeDo.setId(specsCfg.getSpecsId());
        ztreeNodeDo.setpId(specsCfg.getpId());
        ztreeNodeDo.setName(specsCfg.getSpecsName());
        ztreeNodeDo.setT(specsCfg.getSpecsName());
        return ztreeNodeDo;
    }

    @ResponseBody
    @PostMapping("/addSpecs")
    public TbSpecsCfg addSpecs(TbSpecsCfg specsCfg, HttpServletRequest request){
        specsCfg.setAddUserId(getUserId());
        TbSpecsCfg specsCfg1 = specsCfgService.insertSelective(specsCfg);
        return specsCfg1;
    }

    @ResponseBody
    @PostMapping("/onRename")
    public R onRename(TbSpecsCfg TbSpecsCfg, HttpServletRequest request){
        TbSpecsCfg.setUpdateUserId(getUserId());
        int result = specsCfgService.update(TbSpecsCfg);
        return  R.ok();
    }


    @ResponseBody
    @PostMapping("/onRemove")
    public R onRemove(TbSpecsCfg TbSpecsCfg){
        Long delUserId = getUserId();
        int result = specsCfgService.remove(TbSpecsCfg.getSpecsId(),delUserId);
        return  R.ok();
    }
	
}
